﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using Willow.IService.Auth;
using Willow.Model;
using Willow.MVC.Result;

namespace Willow.WebApplication.Areas.Auth.Controllers
{
    public class RoleController : Controller
    {

        #region 属性

        public IRoleService RoleService { get; set; }
        public IRoleModuleService RoleModuleService { get; set; }
        public IRoleActionService RoleActionService { get; set; }

        #endregion


        #region 页面

        public ViewResult Management()
        {
            return View();
        }

        #endregion

        #region 操作

        /// <summary>
        /// 分配角色
        /// </summary>
        /// <param name="userId">The user identifier.</param>
        /// <param name="roleIds">The role ids.</param>
        /// <returns></returns>
        public JsonResult DistributeRole(Guid userId, Guid[] roleIds)
        {
            return this.StandardJson(() =>
            {
                RoleService.DistributeRole(userId, roleIds);
            });
        }

        /// <summary>
        /// 为树获取所有角色
        /// </summary>
        /// <returns></returns>
        public JsonResult GetAllRolesForTree()
        {
            var data = RoleService
                .Filter(p => p.IsDelete == false)
                .OrderBy(p => p.Name)
                .Select(p => new { id = p.Id, text = p.Name })
                .ToList();
            return Json(data, JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// 获取角色分页后信息
        /// </summary>
        /// <param name="page">The page.</param>
        /// <param name="rows">The rows.</param>
        /// <param name="name">The name.</param>
        /// <returns></returns>
        public JsonResult GetPagedList(int page, int rows, string name = "")
        {
            int total = 0;
            var lstData = RoleService.Filter(
                p => p.Name.Contains(name) && p.IsDelete == false,
                out total, page, rows
            ).Select(p => new
            {
                p.Name,
                p.Remark,
                p.IsDelete,
                p.Id
            });
            return Json(new { rows = lstData, total }, JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// 提交角色
        /// </summary>
        /// <param name="lstInsert">The LST insert.</param>
        /// <param name="lstDelete">The LST delete.</param>
        /// <param name="lstUpdate">The LST update.</param>
        /// <returns></returns>
        public JsonResult Submit(List<Auth_Role> lstInsert, List<Auth_Role> lstDelete, List<Auth_Role> lstUpdate)
        {
            return this.StandardJson(() =>
            {
                RoleService.Submit(lstInsert, lstDelete, lstUpdate);
            });
        }

        /// <summary>
        /// 获取角色相关模块和功能
        /// </summary>
        /// <param name="roleId">The role identifier.</param>
        /// <returns></returns>
        public JsonResult GetRelatedModuleAndAction(Guid roleId)
        {
            var data = RoleModuleService.Filter(p => p.RoleId == roleId)
                .Select(p => p.ModuleId).ToList();
            var lstAction = RoleActionService.Filter(p => p.RoleId == roleId)
                .Select(p => p.ActionId).ToList();
            data.AddRange(lstAction);
            return Json(data, JsonRequestBehavior.AllowGet);
        }

        #endregion

    }
}